<template>
	<view v-if="productData != '' ">
		<view class="banner p30 bgw">
			<image :src="productData.store.headImg" mode="aspectFit"></image>
		</view>
		<view class="shop_title p30 bgw ">
			<view @tap="removeFavorite()" v-show="productData.isFavorite" class="storeFavorite">
				<image src="../../static/image/icon_shoucang_select@2x.png" mode=""></image>
					<br/>取消
			</view>
			<view @tap="addFavorite()" v-show="!productData.isFavorite" class="storeFavorite">
				<image src="../../static/image/icon_shoucang_narmal@2x.png" mode=""></image>
					<br/>收藏
			</view>
			<p class="one">
				{{productData.store.name}} 
			</p>
			<view class="shop_txt bb1">
				<span class="stare">
					<image src="../../static/image/star.png" mode=""></image>
					<image src="../../static/image/star.png" mode=""></image>
					<image src="../../static/image/star.png" mode=""></image>
					<image src="../../static/image/star.png" mode=""></image>
					<image src="../../static/image/star.png" mode=""></image> 
					5.0
				</span>
				<span>
					{{productData.store.category.name}}
				</span>
				<span v-if="productData.distance">
					{{productData.distance}}
				</span> 
			</view>
			<view v-if="productData.store.validActivity" class="label">
				<p v-for="(v,i) in productData.store.validActivity.activityItems" :key="i">
					满 {{v.exceedPrice}}减{{v.deductionPrice}} 元
				</p>  
			</view>
			<view class="bottom">
				<view class="address">
					 {{productData.store.address}}
				</view>
				<view class="call">
					<view  @tap="openServe()">
						<image src="../../static/image/yuyin.png" mode=""></image>
						<br /> 语音
					</view>
					<view @tap="call()">
						<image src="../../static/image/dianhua.png" mode=""></image>
						<br /> 电话
					</view>

				</view>
			</view>
		</view>
		<view v-if="productData != '' " class="shopMain">
			<view class="tab ">
				<u-tabs :list="list" active-color="#1BBF80"   :is-scroll="true" :current="current" @change="change"></u-tabs>
			</view>
			<goods ref="goods" v-show="current == 0"></goods>
			<comment v-show="current == 1"></comment>
			<shop v-show="current == 2"></shop>
		</view>
	</view>
</template>

<script>
	import goods from './goods.vue';
	import comment from './comment.vue';
	import shop from './shop.vue'
	import {storeDetail,storeFavoriteAdd,storeFavoriteCancel} from '@/api/api.js'
	import {checkingLogin,toLogin} from '@/tool/index.js'
	export default {
		components:{goods,comment,shop},
		data() {
			return {
				list: [{
					name: '商品'
				}, {
					name: '评价'
				}, {
					name: '商家'
				}],
				current: 0,
				productData:[],
				id:0
			};
		},
		onLoad(e) {
			this.id = e.id 
		},
		onShow() { 
			this.checkingLogin()
		},
		onReachBottom() {
			this.$refs['goods'].checkingAllGoods()
		},
		methods: {
			async removeFavorite(){
				let id = []
				id.push(this.id)
				let data = await storeFavoriteCancel({
					ids:id
				})
				if(data.code == 0){
					this.productData.isFavorite = false
				}
			},
			async addFavorite(){
				let token = uni.getStorageSync('token')
				if (!token) {
					toLogin()
					return
				}
				let data = await storeFavoriteAdd({
					id:this.id
				})
				if(data.code == 0){
					this.productData.isFavorite = true
				}
			},
			async checkingLogin(){
				let checking =await checkingLogin()
				if(checking || !checking){ 
					this.init()
					setTimeout(()=>{
						this.$refs['goods'].inits()
					},400)
				}  
			},
			async init (){
				let data = await storeDetail({
					id:this.id,
					longitude:this.$store.state.longitude,
					latitude:this.$store.state.latitude,
				})
				this.productData = data.data
				uni.setStorageSync('shopdetailsGoodsNav',data.data)
			},
			change(index) {
				this.current = index;
			},
			call(){
				uni.makePhoneCall({
					phoneNumber:this.productData.store.mobile
				})
			},
			openServe() {
				let token = uni.getStorageSync('token')
				if (!token) {
					toLogin()
					return
				}
				let mpId = plus.runtime.appid
				let url = 'eper://weichat.xar.com/tochat?mobile=' + this.productData.store.mobile+'&mpId='+mpId;
				plus.runtime.openURL(url, res => {
					console.log(res);
				});
			},
		}
	}
</script>

<style lang="less" scoped>
	@import './index.less';
</style>
